package com.ruoyi.web.ydz.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.InvitationLike;
import com.ruoyi.web.ydz.mapper.InvitationLikeMapper;
import com.ruoyi.web.ydz.service.IInvitationLikeService;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 提问点赞Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class InvitationLikeServiceImpl implements IInvitationLikeService {
    @Resource
    private InvitationLikeMapper invitationLikeMapper;

    @Resource
    private RedisCache redisCache;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询提问点赞
     *
     * @param id 提问点赞主键
     * @return 提问点赞
     */
    @Override
    public InvitationLike selectInvitationLikeById(Long id) {
        return invitationLikeMapper.selectInvitationLikeById(id);
    }

    /**
     * 查询提问点赞列表
     *
     * @param invitationLike 提问点赞
     * @return 提问点赞
     */
    @Override
    public List<InvitationLike> selectInvitationLikeList(InvitationLike invitationLike) {
        return invitationLikeMapper.selectInvitationLikeList(invitationLike);
    }

    /**
     * 新增提问点赞
     *
     * @param invitationLike 提问点赞
     * @return 结果
     */
    @Override
    public AjaxResult insertInvitationLike(InvitationLike invitationLike) {
        AjaxResult ajaxResult = new AjaxResult();

        try {
            if (Objects.isNull(invitationLike.getInvitationId())) {
                ajaxResult.put("code", 200);
                ajaxResult.put("msg", "题目id不能为空");
                return ajaxResult;
            } else if (Objects.isNull(invitationLike.getStatus())) {
                ajaxResult.put("code", 200);
                ajaxResult.put("msg", "点赞状态不能为空");
                return ajaxResult;
            }

            SysUser user = SecurityUtils.getLoginUser().getUser();
            invitationLike.setCreateTime(DateUtils.getNowDate());
            invitationLike.setCreateBy(user.getUserName());
            invitationLike.setCreateName(user.getNickName());
            invitationLike.setUserId(user.getUserId());

            /*点赞数量*/
            String InvitationLike = "Invitation:InvitationLike:" + invitationLike.getInvitationId();
            Integer currentAge = redisCache.getCacheObject(InvitationLike);

            if (Objects.isNull(currentAge) || Integer.valueOf(currentAge) <= 0) {
                if ("0".equals(invitationLike.getStatus())) {
                    redisCache.setCacheObject(InvitationLike, 1);
                } else if ("1".equals(invitationLike.getStatus())) {
                    /*若是取消点赞*/
                    redisCache.setCacheObject(InvitationLike, 0);
                }
            } else if (Integer.valueOf(currentAge) > 0) {
                if ("0".equals(invitationLike.getStatus())) {
                    redisCache.setCacheObject(InvitationLike, Integer.valueOf(currentAge) + 1);
                } else if ("1".equals(invitationLike.getStatus())) {
                    /*若是取消点赞*/
                    redisCache.setCacheObject(InvitationLike, Integer.valueOf(currentAge) - 1);
                }
            }

            /*记录题目被谁点过赞*/
            String InvitationLikesUser = "Invitation:InvitationLikesUser:" + invitationLike.getInvitationId();
            SetOperations<String, String> listOps = redisTemplate.opsForSet();
            if ("0".equals(invitationLike.getStatus())) {
                listOps.add(InvitationLikesUser, String.valueOf(invitationLike.getUserId())); // 在列表右侧添加元素
                invitationLikeMapper.insertInvitationLike(invitationLike);
            } else if ("1".equals(invitationLike.getStatus())) {
                // 若是取消点赞
                listOps.remove(InvitationLikesUser, 0, String.valueOf(invitationLike.getUserId())); // 0 表示删除所有匹配的值
                invitationLikeMapper.updateInvitationLikeByUserId(invitationLike);
            }

            /*记录点赞操作*/
            String InvitationLikesRecord = "Invitation:InvitationLikesRecord:" + invitationLike.getInvitationId();
            ListOperations<String, String> recordlist = redisTemplate.opsForList();
            recordlist.rightPush(InvitationLikesRecord, JSONObject.toJSONString(invitationLike)); // 在列表右侧添加元素

            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "0".equals(invitationLike.getStatus()) ? "点赞操作成功" : "取消点赞成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.put("code", 500);
            ajaxResult.put("msg", "操作失败,请联系管理员");
            ajaxResult.put("error", e.getMessage());
        }
        //return invitationLikeMapper.insertInvitationLike(invitationLike);
        return ajaxResult;
    }

    /**
     * 修改提问点赞
     *
     * @param invitationLike 提问点赞
     * @return 结果
     */
    @Override
    public int updateInvitationLike(InvitationLike invitationLike) {
        invitationLike.setUpdateTime(DateUtils.getNowDate());
        return invitationLikeMapper.updateInvitationLike(invitationLike);
    }

    /**
     * 批量删除提问点赞
     *
     * @param ids 需要删除的提问点赞主键
     * @return 结果
     */
    @Override
    public int deleteInvitationLikeByIds(Long[] ids) {
        return invitationLikeMapper.deleteInvitationLikeByIds(ids);
    }

    /**
     * 删除提问点赞信息
     *
     * @param id 提问点赞主键
     * @return 结果
     */
    @Override
    public int deleteInvitationLikeById(Long id) {
        return invitationLikeMapper.deleteInvitationLikeById(id);
    }

    /*根据帖子ID获取点赞总数*/
    @Override
    public HashMap<String, Object> getinvitationLikes(Long invitationLikeId) {
        HashMap<String, Object> hashMap = new HashMap<>();

        Object cacheObject = redisCache.getCacheObject("Invitation:InvitationLike:" + invitationLikeId);
        if (!Objects.isNull(cacheObject)) {
            hashMap.put("likesCount", cacheObject);
        }
        return hashMap;
    }
}
